package com.itic.develop.weeks.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.exports.ExportExcel;
import com.itic.appbase.applications.exports.ExportExcelWithTemplet;
import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.basevo.User;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.develop.weeks.persistence.model.Weeks;
import com.itic.develop.weeks.persistence.model.WeeksExample;
import com.itic.develop.weeks.persistence.model.WeeksExample.Criteria;
import com.itic.develop.weeks.service.WeeksService;

@Controller
@RequestMapping(value = "/develop/weeks")
public class WeeksController extends BaseController {
	@Autowired
	private WeeksService weeksService;

	@Autowired
	protected OutCommonService outCommonService;

	////////////////////////////////////////////////////// 常量区////////////////////////////////////////////////////////////////
	private static final String MODEL_NAME_CHINA = "周报管理";// 中文名
	private static final String MODEL_NAME＿ENGLISH = "weeks";// 英文名
	private static final String PAGE_COMMON_PATH = "/develop/weeks/weeks";// 通用路径
	private static final String COMMON_URL = "/develop/weeks/";// 通用路径

	// 导出常量
	private static final String TEMP = "bzmc";// 备注名称
	private static final String DATALIST = "dataList";// 数据常量
	// 返回属性黑名单
	private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "createTime", "updateTime","orgCode","reorder","note","question","study","plan","isDone" };

	////////////////////////////////////////////////////// 页面跳转区////////////////////////////////////////////////////////////

	/*
	 * 主页面
	 */
	@RequestMapping(value = "/init")
	public String init(Model model) {
		model.addAttribute("currentDay",DateHelper.getDate());
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		return PAGE_COMMON_PATH + "Main";
	}

	/*
	 * 添加页面
	 */
	@RequestMapping(value = "/initAdd")
	public String initAdd(HttpServletRequest request,Model model,Long date) {
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		
		Date date2 = new Date(date);
		String dateTemp = DateHelper.formatDate(date2,"yyyy-MM-dd");
		Weeks editData = getEditDateByDate(request,dateTemp);
		if( null != editData ){
			model.addAttribute("editData", iticJsonMapper.toJson(editData));
			return PAGE_COMMON_PATH + "Edit";
		}else{
			model.addAttribute("date", dateTemp);
			return PAGE_COMMON_PATH + "Add";
		}
	}

	/**
	 * 是否有数据
	 * @param request
	 * @param dateTemp
	 * @return
	 */
	private Weeks getEditDateByDate(HttpServletRequest request, String dateTemp) {
		Weeks weeks = null;
		// 过滤组织机构
	    User user = findUserSession(request);
	    WeeksExample example = new WeeksExample();
	    Criteria criteria = example.createCriteria();
	    criteria.andIsEffectiveEqualTo(DBConst.TRUE);
	    criteria.andCreateUserEqualTo(user.getId());
	    criteria.andStartEqualTo(dateTemp);
	    
	    List<Weeks> list = weeksService.list(example);
	    if( list.size()>0 ){
	    	weeks = list.get(0);
	    }
		return weeks;
	}

	/*
	 * 编辑页面
	 */
	@RequestMapping(value = "/initEdit")
	public String initEdit(String id, Model model) {
		if (StringHelper.isNotBlank(id)) {
			Weeks temp = weeksService.get(id);
			model.addAttribute("editData", iticJsonMapper.toJson(temp));
			model.addAttribute("name_china", MODEL_NAME_CHINA);
			model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
			model.addAttribute("common_url", COMMON_URL);
		} else {
			throw new ParameterErrorException();
		}
		return PAGE_COMMON_PATH + "Edit";
	}
	
	/*
	 * 编辑页面
	 */
	@RequestMapping(value = "/initSave")
	public String initSave(String id, Model model) {
		if (StringHelper.isNotBlank(id)) {
			Weeks temp = weeksService.get(id);
			model.addAttribute("editData", iticJsonMapper.toJson(temp));
		}
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		return PAGE_COMMON_PATH + "Save";
	}

	/*
	 * 查看页面
	 */
	@RequestMapping(value = "/initView")
	public String initView(String id, Model model) {
		if (StringHelper.isNotBlank(id)) {

			Weeks temp = weeksService.get(id);

			model.addAttribute("viewEdit", iticJsonMapper.toJson(temp));
			model.addAttribute("name_china", MODEL_NAME_CHINA);
			model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
			model.addAttribute("common_url", COMMON_URL);
		} else {
			throw new ParameterErrorException();
		}

		return PAGE_COMMON_PATH + "View";
	}

	/*
	 * 导入页面
	 */
	@RequestMapping(value = "/initImp")
	public String initImp(Model model) {
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		return PAGE_COMMON_PATH + "Imp";
	}

	////////////////////////////////////////////////////// 业务代码区////////////////////////////////////////////////////////////

	/**
	 * 添加模板管理数据
	 * 
	 * @param android
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, String> add(Weeks temp) {

		Map<String, String> resultMap = new HashMap<String, String>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		//String unescapeXml = StringEscapeUtils.unescapeXml(temp.getStyleText());
		//temp.setStyleText(unescapeXml);
		if( StringHelper.isBlank(temp.getTitle()) ){
			temp.setTitle(temp.getPlan().substring(5));
		}
		result = weeksService.add(temp);

		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

	/**
	 * <p>
	 * Title:queryCondition
	 * </p>
	 * <p>
	 * Description:封装查询条件
	 * </p>
	 * 
	 * @param jzdw
	 * @param jzdwExample
	 * @return
	 */
	private WeeksExample queryCondition(HttpServletRequest request, Weeks temp) {

		// 参数处理
		WeeksExample example = new WeeksExample();
		// 按创建时间倒序
		example.setOrderByClause("create_time desc");
		// 按排列号顺序
		example.setOrderByClause("reorder asc");

		Criteria criteria = example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);

		// 过滤组织机构
	    User user = findUserSession(request);
	    
		criteria.andCreateUserEqualTo(user.getId());

		// 查询条件
		if (null != temp) {
			
		}
		return example;
	}

	/**
	 * 
	 * @param goldType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public List<Map<String,Object>> list(Weeks temp, HttpServletRequest request) {

		WeeksExample example = this.queryCondition(request, temp);
		// 封装分页参数
		example = this.handlePageQuery(request, example);

		// 获取数据
		List<Weeks> list = weeksService.list(example);

		return ValueHelper.tranList2MapList(list, ignoreProperties);

	}

	/**
	 * 编辑保存
	 * 
	 * @param Weeks
	 * @return
	 */
	@RequestMapping(value = "/edit")
	@ResponseBody
	public Map<String, String> edit(String delFileIds, Weeks temp) {

		if (StringHelper.isNotBlank(delFileIds)) {
			// 删除附件
			outCommonService.deleteFiles(delFileIds);
		}

		Map<String, String> resultMap = new HashMap<String, String>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		//String unescapeXml = StringEscapeUtils.unescapeXml(temp.getStyleText());
		//temp.setStyleText(unescapeXml);
		result = weeksService.edit(temp);

		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, String> del(String id) {
		Map<String, String> resultMap = new HashMap<String, String>();
		String result = ResponseCode.SERVER_ERROR.getCode();

		// 删除一个
		if (StringHelper.isNotBlank(id)) {
			Weeks temp = new Weeks();
			temp.setId(id);
			result = weeksService.delete(temp);
		}

		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

	/**
	 * exp
	 * 
	 */
	@RequestMapping(value = "/exp")
	@ResponseBody
	public void exp(HttpServletRequest request, HttpServletResponse response, Weeks temp) {
		try {
			// 参数处理
			WeeksExample example = new WeeksExample();
			example = this.queryCondition(request, temp);
			// 获取数据
			List<Weeks> list = weeksService.list(example);
			// 导出
			Map<String, Object> exportMap = new HashMap<String, Object>();
			exportMap.put(TEMP, MODEL_NAME＿ENGLISH);
			exportMap.put(DATALIST, list);
			ExportExcel exportExcel = new ExportExcelWithTemplet();
			exportExcel.exportExcel(exportMap, request, response);

		} catch (Exception e) {
			throw new RuntimeException("[WeeksController | exp 模板示例导出异常]");
		}
	}

}